import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { svgBuilder } from './src/plugins/svgBuilder';

//直接获取文件的text
function rawTransform(
  fileRegex: Array<RegExp>,
): {
  name: string;
  transform: (src: string, id: string) => string | void;
} {
  return {
    name: 'get-file-raw',
    transform(src, id): string | void {
      if (fileRegex.filter((re) => re.test(id)).length > 0) {
        return `export default ${JSON.stringify(src)}`;
      }
    },
  };
}

// https://vitejs.dev/config/
export default defineConfig({
  optimizeDeps: {
    //声明深度路径模块
    include: [
      'bpmn-js/lib/Modeler',
      'highlight.js',
      'codemirror',
      'codemirror/mode/xml/xml.js',
      'codemirror/addon/hint/xml-hint.js',
      'bpmn-js/lib/features/label-editing/LabelUtil.js',
    ],
  },
  plugins: [vue(), rawTransform([/\.bpmn$/]), vueJsx({}),svgBuilder('./src/icons/svg/')],
  resolve: {
    alias: {
        '@': path.join(__dirname, 'src')
    }
},
css: {
    preprocessorOptions: {
        less: {
            modifyVars: {
                'primary-color': '#1890ff',
                'link-color': '#1890ff',
                'border-radius-base': '4px'
            },
            javascriptEnabled: true
        }
    }
},
server: {
  open: true,
    proxy: {
        // 选项写法
        '/api': { 
            target: 'http://localhost:5000', // 本地 
            //  rewrite: path => path.replace(/^\/api/, ''), // 重写传过来的path路径，比如 `/api/index/1?id=10&name=zs`（注意:path路径最前面有斜杠（/），因此，正则匹配的时候不要忘了是斜杠（/）开头的；选项的 key 也是斜杠（/）开头的）
            changeOrigin: true // true/false, Default: false - changes the origin of the host header to the target URL
        }
    }
}
})
